<template>
  <el-row type="flex" class="login" justify="center" align="middle">
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="用户名">
        <el-input v-model="form.username" placeholder="请输入账户"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="login">登录</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
  </el-row>
</template>

<script>
export default {
  data() {
    return {
      form: {
        username: "",
        password: ""
      }
    };
  },
  methods: {
    login() {
      const username = this.form.username;
      const password = this.form.password;
      // 100867
      // 123

      this.$axios({
        method: "post",
        url: "/login",
        data: {
          username: username,
          password: password
        }
      }).then(res => {
        console.log(res);

        const { message, statusCode, data } = res.data;
        if (statusCode) {
          this.$message.error(message);
        } else {
          this.$message.success(message);
          localStorage.setItem("token", data.token);
          localStorage.setItem("user_id", data.user.id);
          this.$router.push({ path:'/' })
        }
      });
    },
    reset() {
      this.form.username = "";
      this.form.password = "";
    }
  }
};
</script>

<style lang="less" scoped>
.login {
  width: 100%;
  position: absolute;
  top: 0;
  bottom: 0;
}
</style>